package per.turtle.flowable.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import per.turtle.common.core.web.entity.CommonResult;
import per.turtle.flowable.domain.vo.FlowTaskVo;
import per.turtle.flowable.service.IFlowInstanceService;

import java.util.Map;

/**
 * <p>工作流流程实例管理<p>
 *
 * @author XuanXuan
 */
@Slf4j
@Tag(name = "工作流流程实例管理")
@RestController
@RequestMapping("/flowable/instance")
public class FlowInstanceController {

    @Autowired
    private IFlowInstanceService flowInstanceService;

    @Operation(summary = "删除流程实例")
    @DeleteMapping(value = "/delete")
    public CommonResult delete(@Parameter(name = "流程实例ID", required = true) @RequestParam String instanceId,
                               @Parameter(name = "删除原因") @RequestParam(required = false) String deleteReason) {
        flowInstanceService.delete(instanceId, deleteReason);
        return CommonResult.success();
    }

    @Operation(summary = "根据流程定义id启动流程实例")
    @PostMapping("/startBy/{procDefId}")
    public CommonResult startById(@Parameter(name = "流程定义id") @PathVariable(value = "procDefId") String procDefId,
                                  @Parameter(name = "变量集合,json对象") @RequestBody Map<String, Object> variables) {
        return flowInstanceService.startProcessInstanceById(procDefId, variables);

    }

    @Operation(summary = "结束流程实例")
    @PostMapping(value = "/stopProcessInstance")
    public CommonResult stopProcessInstance(@RequestBody FlowTaskVo flowTaskVo) {
        flowInstanceService.stopProcessInstance(flowTaskVo);
        return CommonResult.success();
    }

    @Operation(summary = "激活或挂起流程实例")
    @PostMapping(value = "/updateState")
    public CommonResult updateState(@Parameter(name = "1:激活,2:挂起", required = true) @RequestParam Integer state,
                                    @Parameter(name = "流程实例ID", required = true) @RequestParam String instanceId) {
        flowInstanceService.updateState(state, instanceId);
        return CommonResult.success();
    }

}